<?php
/**
* @file Quotepostm.php
* @synopsis  用户发帖
* @author Yee, <rlk002@gmail.com>
* @version 1.0
* @date 2015-03-11 17:31
*/


class Quotepostm extends CI_Model
{
	function __construct()
	{
		$this->load->model('commentm');
		$this->load->model('userm');
	}

	//获取帖子id 数组
	public function getuserpostlist($user_id,$addtime,$start,$page_size)
	{

		$this->db->select('ml_post.id,ml_post.addtime,ml_attachments.small');
		$this->db->from('ml_post');
		$this->db->join('ml_attachments', 'ml_attachments.pid=ml_post.id','left');
		$this->db->where("ml_post.is_show",1);
		$this->db->where("ml_attachments.cid",0);
		$this->db->where("ml_attachments.sort",1);
		$this->db->where("ml_post.author_id",$user_id);
		if($addtime)
		{
			$this->db->where("ml_post.addtime<",$addtime);
		}
		$this->db->order_by("ml_post.addtime","desc");
		$this->db->limit($page_size,$start);


		$query_result = $this->db->get()->result_array();

		$return = array();
		foreach ($query_result as $value) {
			$item = array();
			$item['id'] = $value['id'];
			$item['addtime'] = $value['addtime'];
			$item['small'] = ret_fullpath_url($value['small']);
			$return[] = $item;
		}
		return $return;
	}

	//获取帖子id 数组
	public function getcommentpostlist($user_id,$comment_addtime,$start,$page_size)
	{

		$this->db->select('ml_comment.post_id,ml_comment.addtime as comment_addtime,ml_attachments.small');
		$this->db->from('ml_comment');
		$this->db->join('ml_post', 'ml_comment.post_id=ml_post.id','left');
		$this->db->join('ml_attachments', 'ml_post.id=ml_attachments.pid','left');
		$this->db->where("ml_comment.is_show",1);
		$this->db->where("ml_post.is_show",1);
		$this->db->where("ml_attachments.cid",0);
		$this->db->where("ml_attachments.sort",1);
		$this->db->where("ml_comment.author_id",$user_id);
		if($comment_addtime)
		{
			$this->db->where("ml_comment.addtime<",$comment_addtime);
		}
		$this->db->group_by("ml_comment.post_id");
		$this->db->order_by("ml_comment.addtime","desc");
		$this->db->limit($page_size,$start);

		$query_result = $this->db->get()->result_array();

		$return = array();
		foreach ($query_result as $value) {
			$item = array();
			$item['post_id'] = $value['post_id'];
			$item['comment_addtime'] = $value['comment_addtime'];
			$item['small'] = ret_fullpath_url($value['small']);
			$return[] = $item;
		}

		return $return;
	}

	/*//获取帖子id 数组
	public function getfavoritepostlist($user_id,$favorite_addtime,$start,$page_size)
	{
		//
	}	*/
	

	//获取发帖总数
	public function get_sum($user_id)
	{
		$return = array();

		$query = array("author_id"=>$user_id,"is_show"=>1, 'post_type <>' => 1);
		$return['userpostcount'] = $this->db->where($query)->where_in('post_type_extend', array(0,1,2,3))->count_all_results("ml_post");

		$return['commentpostcount'] = $this->commentm->get_user_comment_count($user_id, 'medical');

		//收藏的帖子数
        $return['favoritepostcount'] = $this->favoritem->get_user_fav_count($user_id, 1);

        return $return;

	}
	
}
